package ems;

import util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @Author:石小俊
 * @Date:2022/12/9 11:09
 * @Version:1.0-SNAPSHOT
 * @Description:
 */
@WebServlet("/showModifyEmp")
public class ShowModifyEmpServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        String id = request.getParameter("id");
        System.out.println("id:"+id);
        // JDBC操作,根据主键查询员工信息
        // 设置默认值
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/web?useUnicode=true&characterEncoding=utf-8","root","");
            conn.setAutoCommit(false);
            String sql = new StringBuffer()
                    .append(" select id,name,salary,gender,edu,hobbies ")
                    .append(" from t_emp ")
                    .append(" where id = ? ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setInt(1,Integer.parseInt(id));
            rs = ps.executeQuery();
            while(rs.next()){
                out.print(" <!DOCTYPE html> ");
                out.print(" <html lang=\"en\"> ");
                out.print(" <head> ");
                out.print("     <meta charset=\"UTF-8\"> ");
                out.print("     <title>修改员工信息</title> ");
                out.print(" </head> ");
                out.print(" <body> ");
                out.print(" <h1>修改员工</h1> ");
                out.print(" <a href=\"findAll\">返回员工列表</a> ");
                out.print(" <hr> ");
                out.print(" <form action=\"modifyEmp\" method=\"post\"> ");
                out.print("     编号:<input type=\"text\" name=\"id\" value=\""+rs.getInt("id")+"\" readonly><br> ");
                out.print("     姓名:<input type=\"text\" name=\"name\" value=\""+rs.getString("name")+"\"><br> ");
                out.print("     工资:<input type=\"text\" name=\"salary\" value=\""+rs.getDouble("salary")+"\"><br> ");
                out.print("     性别: ");
                out.print("     <input type=\"radio\" value=\"man\" name=\"gender\" "+(rs.getString("gender").equals("man") ? "checked" : "")+">男 ");
                out.print("     <input type=\"radio\" value=\"woman\" name=\"gender\" "+(rs.getString("gender").equals("woman") ? "checked" : "")+">女<br> ");
                out.print("     学历: ");
                out.print("     <select name=\"edu\"> ");
                out.print("         <option value=\"-1\">--请选择--</option> ");
                out.print("         <option value=\"1\" "+("1".equals(rs.getString("edu")) ? "selected" : "")+">大专</option> ");
                out.print("         <option value=\"2\" "+("2".equals(rs.getString("edu")) ? "selected" : "")+">本科</option> ");
                out.print("         <option value=\"3\" "+("3".equals(rs.getString("edu")) ? "selected" : "")+">研究生</option> ");
                out.print("     </select><br> ");
                out.print("     兴趣: ");
                out.print("     <input type=\"checkbox\" name=\"hobbies\" value=\"1\" "+ StringUtil.checkHobby(rs.getString("hobbies"),"1") +">吃饭 ");
                out.print("     <input type=\"checkbox\" name=\"hobbies\" value=\"2\" "+StringUtil.checkHobby(rs.getString("hobbies"),"2")+">睡觉 ");
                out.print("     <input type=\"checkbox\" name=\"hobbies\" value=\"3\" "+StringUtil.checkHobby(rs.getString("hobbies"),"3")+">打豆豆<br> ");
                out.print("     <input type=\"submit\" value=\"修改\"> ");
                out.print(" </form> ");
                out.print(" </body> ");
                out.print(" </html> ");
            }

            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }


    }

}
